from rest_framework import serializers
from goods.models import GoodCategoryVisit, SKU, SKUSpecification, GoodsCategory, SPU
#1.定义日分类访问量,序列化器
from users.models import User


class UserGoodsDaySerializer(serializers.ModelSerializer):
    category=serializers.StringRelatedField(read_only=True)
    class Meta:
        model=GoodCategoryVisit
        fields=("category","count")

#2.获取用户信息,序列化器
class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model=User
        fields=('id','username','mobile','email')

#3.新增用户,序列化器
class UserAddSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'username', 'mobile', 'email','password')

        #1添加额外的约束
        extra_kwargs={
            "password":{
                "write_only":True
            }
        }

    #1.重写create方法,加密密码
    def create(self, validated_data):
        return User.objects.create_user(**validated_data)

#4.商品sku的规格信息,序列化器
class SKUSpecificationSerializer(serializers.Serializer):
    spec_id = serializers.IntegerField()
    option_id= serializers.IntegerField()
    # class Meta:
    #     model=SKUSpecification
    #     fields=("spec_id","option_id")

#4.1获取SKU信息的序列化器
class SKUSerializer(serializers.ModelSerializer):
    #1.重写sku信息的序列化器
    spu=serializers.StringRelatedField(read_only=True)
    spu_id=serializers.IntegerField()
    #2.重写category,catgory_id
    category=serializers.StringRelatedField(read_only=True)
    category_id=serializers.IntegerField()

    #3.specs
    specs=SKUSpecificationSerializer(read_only=True,many=True)
    class Meta:
        model=SKU
        fields="__all__"
#5.获取sku的三级分类,序列化器
class SKUCategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = GoodsCategory
        fields=("id","name")

#6.sku简要信息序列化器
class GoodSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model=SPU
        fields=("id","name")





